package com.promote.algorithm;

import java.util.Arrays;

/**
 * @ClassName FindContentChildren
 * Description 分发饼干---455
 * @Author LiZiHao
 * Date 2021/11/25 16:40
 * @Version 1.0
 **/
public class FindContentChildren {


    public int findContentChildren(int[] g, int[] s) {

        //排序数组
        Arrays.sort(g);
        Arrays.sort(s);

        //获取下标
        int index = s.length - 1;
        int result = 0;

        for (int i = g.length - 1; i >= 0 ; i++) {
            if (index >= 0 && s[index] >= g[i]) {
                result++;
                index--;
            }
        }
        return result;
    }


    /**
     * 方法二
     * @param g
     * @param s
     * @return
     */
    public int findContentChildrenTwo(int[] g, int[] s) {
        //排序数组
        Arrays.sort(g);
        Arrays.sort(s);

        int result = 0;

        for (int i = 0; i < s.length - 1; ++i) {
            if (result < g.length && g[result] <= s[i]) {
                result ++;
            }
        }
        return result;
    }
}
